import {IS_DEBUG} from '@/env'

export default {
    doRequest(method, url, data, contentType, type = true) {
        if (IS_DEBUG) {
            var apiurl = "https://teststate.renruikeji.cn/api/userservice";
            // var apiurl = "http://userservice.renrui.net/userservice";
        } else {
            // var apiurl="https://state.renkangshangcheng.cn/api/userservice/tags";
            var apiurl = "https://state.renruikeji.cn/api/userservice";
        }
        var token = uni.getStorageSync('token');
        if (type) {
            uni.showLoading({
                title: "加载中"
            })
        }
        return new Promise((resolve, reject) => {
            let header = {}
            if (contentType === 'json') {
                header = {
                    'content-type': 'application/json',
                    'Authorization': token,
                    'token': token,
                }
            } else {
                header = {
                    'content-type': 'application/x-www-form-urlencoded',
                    'Authorization': token,
                    'token': token,
                }
            }
            uni.request({
                method: method,
                url: apiurl + url,
                data: data,
                header: header,
                success: function (result) {
                    uni.hideLoading();
                    if (result.data.code == 401) {
                        uni.removeStorageSync('token')
                    }
                    if (result.data.code == 400) {
                        if (token) {
                            uni.showToast({
                                title: result.data.msg,
                                icon: 'none'
                            })
                        }
                    }
                    resolve(result)
                },
                fail: function (e) {
                    reject(e)
                },
            })
        })

    },

}
